/**
 * 发送短信
 * 
 * @return {}
 */
function addSendSms() {
	var addSendSmsForm = new Ext.form.FormPanel({
		id : 'addSendSmsForm',
		frame : true,
		layout : "form",
		buttonAlign : 'center',
		monitorValid : true,
		bodyBorder : false,
		fileUpload : true,// 上传文件设置为true
		items : [{
					xtype : 'localcommonbox',
					id : 'addSendSmsForm_gateWay',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>发送方式",
					allowBlank : false,
					hiddenName : 'gateWay',
					loaclarr : [[0, '输入手机号'], [1, '导入EXCEL'], [2, '群发']],
					value : 0,
					listeners : {
						'select' : function(obj) {
							var gateWay = obj.value;
							controlShow(gateWay);
						}
					}
				}, {
					id : 'addSendSmsForm_phoneNumber',
					xtype : 'textarea',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>用户手机号(多个以英文逗号分隔)",
					height : 100,
					allowBlank : false,
					name : "phoneNumber"
				}, {
					id : 'addSendSmsForm_upload',
					fieldLabel : "<span style='color:#F00'>*</span>导入手机号",
					name : 'phoneNumber',
					xtype : 'fileuploadfield',
					allowBlank : false,
					buttonText : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;浏览&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
					blankText : "请选择您要导入的文件（.xls）",
					width : 300,
					border : false,
					listeners : {
						'fileselected' : function(field, value) {
							var uploadImage = Ext
									.getCmp("addSendSmsForm_upload");
							var img_reg = /\.([xX][lL][sS]){1}$/;
							// 得到选择的图片路径
							var url = 'file://' + uploadImage.getValue();
							if (img_reg.test(url)) {
								smsFileUpload(url);
							} else {
								Ext.MessageBox.show({
									title : "错误提示",
									msg : '请先浏览选择文件<br/>并且文件格式必须是<font color="red">.xls</font>结尾!',
									buttons : Ext.MessageBox.OK,
									icon : Ext.MessageBox.ERROR
								});
							}

						}
					}
				}, {
					id : 'addSendSmsForm_content',
					xtype : 'textarea',
					anchor : '100%',
					allowBlank : false,
					fieldLabel : "<span style='color:#F00'>*</span>短信内容",
					height : 100,
					name : "content"
				}],
		buttons : [{
					text : '确定',
					formBind : true,
					handler : function() {
						addSendUserSms();
					}
				}, {
					text : "取消",
					handler : function() {
						addSendSmsWindow.close();
					}
				}]
	});
	var addSendSmsWindow = new Ext.Window({
		id : 'addSendSmsWindow',
		closeAction : 'close',
		title : '发送短信<span style="color:#F00">(Excel导入每次发送不要超过500个手机号;发送内容需提前在运营商备案)</span>',
		closable : true,
		plain : true,
		modal : true,
		width : 500,
		draggable : false, // 不可拖动
		resizable : false, // 不可调整
		height : 340,
		buttonAlign : 'center',
		autoDestroy : true,
		layout : 'fit',
		items : [addSendSmsForm],
		listeners : {
			'show' : function() {
				var gateWay = Ext.getCmp("addSendSmsForm_gateWay").getValue();
				controlShow(gateWay);
			},
			'beforeshow' : function() {
				addSendSmsWindow.center();
			}
		}
	});
	return addSendSmsWindow;

};

/**
 * 控制显示
 * 
 * @param {}
 *            gateWay
 */
function controlShow(gateWay) {
	var phoneNumberCon = Ext.getCmp("addSendSmsForm_phoneNumber");
	var uploadCon = Ext.getCmp("addSendSmsForm_upload");
	if (gateWay == 0) {// 手动输入手机号
		phoneNumberCon.getEl().up('.x-form-item').setDisplayed(true);
		uploadCon.getEl().up('.x-form-item').setDisplayed(false);
		phoneNumberCon.allowBlank = false;
		uploadCon.allowBlank = true;
	} else if (gateWay == 1) {
		phoneNumberCon.getEl().up('.x-form-item').setDisplayed(false);
		uploadCon.getEl().up('.x-form-item').setDisplayed(true);
		phoneNumberCon.allowBlank = true;
		uploadCon.allowBlank = false;
	} else if (gateWay == 2) {
		phoneNumberCon.getEl().up('.x-form-item').setDisplayed(false);
		uploadCon.getEl().up('.x-form-item').setDisplayed(false);
		phoneNumberCon.allowBlank = true;
		uploadCon.allowBlank = true;
	}
}
/**
 * 发送短信导入Excel
 * 
 * @param {}
 *            filestr
 */
function smsFileUpload(filestr) {
	var form = Ext.getCmp("addSendSmsForm");
	form.getForm().submit({
				url : __ctxPath + '/smsManager/importExcelUserPhone',
				timeout : 18000000,
				waitTitle : '请稍候',
				waitMsg : '正在导出用户数据，请稍候......',
				success : function(response, config) {
					var re = config.result.userStr
					Ext.getCmp("addSendSmsForm_upload").setValue(re);
				},
				failure : function(response) {
					Ext.getCmp("addSendSmsForm_upload").setValue();
					Ext.MessageBox.show({
								title : "错误提示",
								msg : "导入失败!",
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.ERROR
							});

				}
			});
};
/**
 * 发送短信
 */
function addSendUserSms() {
	var smsForm = Ext.getCmp("addSendSmsForm");
	var gateWay = Ext.getCmp("addSendSmsForm_gateWay").getValue();
	var userPhone = Ext.getCmp("addSendSmsForm_upload").getValue();
	if (gateWay == 1 && userPhone == '') {
		Ext.Msg.show({
					title : '提示信息',
					msg : '请导入用户手机号!',
					buttons : Ext.MessageBox.OK,
					icon : Ext.MessageBox.WARNING
				});
		return;
	}
	if (smsForm.getForm().isValid()) {
		Ext.Msg.confirm("提示框", "确认要发送信息吗?", function(button) {
					if (button == "yes") {
						var smsSended = new Ext.LoadMask(Ext.getBody(), {
									msg : "正在发送,请稍后..."
								});
						smsSended.show();
						Ext.Ajax.request({
									url : __ctxPath + '/smsManager/addSendSms',
									params : {
										gateWay : Ext
												.getCmp("addSendSmsForm_gateWay")
												.getValue(),
										phoneNumber : Ext
												.getCmp("addSendSmsForm_phoneNumber")
												.getValue(),
										content : Ext
												.getCmp("addSendSmsForm_content")
												.getValue()
									},
									method : "POST",
									success : function(response, options) {
										smsSended.hide();
										var jsonData = Ext.util.JSON
												.decode(response.responseText);
										if (jsonData.success) {
											Ext.MessageBox.show({
														title : "信息提示",
														msg : jsonData.data,
														buttons : Ext.MessageBox.OK,
														icon : Ext.MessageBox.INFO
													});
											Ext.getCmp("addSendSmsWindow")
													.close();
											var gridPanel = Ext
													.getCmp('smsManagerGrid');
											if (typeof(gridPanel) != 'undefined') {
												gridPanel.store.reload();
											}
										} else {
											Ext.MessageBox.show({
														title : "操作信息",
														msg : jsonData.data,
														buttons : Ext.MessageBox.OK,
														icon : Ext.MessageBox.ERROR
													});
										}
									},
									failure : function(response, options) {
										smsSended.hide();
										Ext.MessageBox.show({
													title : "错误提示",
													msg : "服务器请求异常,请稍后再试!",
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.ERROR
												});
									}
								});
					}
				});
	}
}